package com.lwz.laboratory.controller;


import com.lwz.laboratory.entity.Department;
import com.lwz.laboratory.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @GetMapping("/departmentlist")
    public String departmentlist(Model model, HttpServletRequest request){
        String departmentname = request.getParameter("context");
        List<Department> list= departmentService.getAlldep(departmentname);
        model.addAttribute("departmentlist",list);
        return "/department/department_list";
    }

    @RequestMapping("/departmentlist/{id}")
    public String toupdatedepartment(@PathVariable("id")Integer id, Model model, HttpServletRequest request){
       Department department=  departmentService.getonedep(id);
        model.addAttribute("department",department);
        return "/department/department_edit";
    }

    @RequestMapping("/departmentedit")
    public String editdepartment(Department department, Model model, HttpServletRequest request){
        departmentService.Updatedep(department);
        return "redirect:/departmentlist";
    }

    @RequestMapping("/departmentadd")
    public String toAdddepartment( HttpServletRequest request){
        return "/department/department_add";
    }

    @RequestMapping("/depsave")
    public String Adddepartment(Department department, HttpServletRequest request){
        departmentService.Adddep(department);
        return "redirect:/departmentlist";
    }


    @RequestMapping("/departmentdel/{id}")
    public String deldepartment(@PathVariable("id")Integer id, Model model, HttpServletRequest request){
        departmentService.Deldep(id);
        return "redirect:/departmentlist";
    }

}
